package net.caixiaomi.info.Lottery.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.caixiaomi.info.Lottery.callback.LotteryBallBettingCallBack;
import net.caixiaomi.info.Lottery.callback.LotteryMainCallBack;
import net.caixiaomi.info.Lottery.callback.TrendCallBack;
import net.caixiaomi.info.Lottery.dialog.TrendSettingDialog;
import net.caixiaomi.info.Lottery.fragment.TrendChartFragment;
import net.caixiaomi.info.Lottery.fragment.TrendMainFragment;
import net.caixiaomi.info.Lottery.model.LotteryTicketInfo;
import net.caixiaomi.info.Lottery.model.LotteryTrendParamsEntity;
import net.caixiaomi.info.Lottery.model.TrendChartData;
import net.caixiaomi.info.Lottery.presenter.LotteryBettingPresenter;
import net.caixiaomi.info.Lottery.presenter.LotteryTrendPresenter;
import net.caixiaomi.info.Lottery.view.LotteryEasyBetView;
import net.caixiaomi.info.Lottery.view.TrendSelectedView;
import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.BaseAlertDialog;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.ui.dialog.MatchFilterDialogFragment;
import net.caixiaomi.info.ui.football.PaymentActivity;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.EventWhat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by suntian on 2018/8/15.
 */

public class LotteryTrendActivity extends BaseActivity implements View.OnClickListener,LotteryMainCallBack,MatchingSelectCallBack,TrendCallBack {

    private Context mContext;
    private boolean mStandardCorrect=false;   //标准是否可以投注
    private String mStandardDes="<html><body>请至少选择<font color='red'>5</font>个红球 <font color='blue'>2</font>个蓝球 </body></html>";


    @BindView(R.id.lottery_easy_bet)
    LotteryEasyBetView mEasyBetView;

    @BindView(R.id.ball_total_des)
    TextView mTotalDesTv;

    @BindView(R.id.ball_lottery_confirm)
    RelativeLayout mConfirmBtn;

    @BindView(R.id.scroll_view)
    HorizontalScrollView scroll_view;

    @BindView(R.id.trend_select_view)
    TrendSelectedView mTrendSelectView;

    @BindView(R.id.trend_bottom_betting_layout)
    LinearLayout mBottomBetting;

    @BindView(R.id.trend_bottom_timer_layout)
    RelativeLayout mBottomTimer;

    @BindView(R.id.trend_bottom_timer_tv)
    TextView mBottomTimerTv;

    private int mCurrentPosition=0;

    @OnClick(R.id.trend_setting)
    void trendSetting(){
        TrendSettingDialog dialog = new TrendSettingDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("params",mPresenter.getmParams());
        dialog.setArguments(bundle);
        dialog.show(getSupportFragmentManager(), TrendSettingDialog.class.getName());
    }

    private TrendMainFragment mFragment0;
    private TrendChartFragment mFragment1;
    private TrendChartFragment mFragment2;
    private TrendMainFragment mFragment3;
    private TrendMainFragment mFragment4;
    private FragmentManager manager;

    private LotteryTrendPresenter mPresenter;
    private TrendChartData mData;

    @OnClick(R.id.ball_lottery_confirm)
    void submit(){
        if(mStandardCorrect) {
            mEasyBetView.addBetingData();
            LotteryBettingPresenter presenter=new LotteryBettingPresenter(this, new LotteryBallBettingCallBack() {
                @Override
                public void betSUccess(String patToken) {
                    Intent intent=new Intent(mContext,PaymentActivity.class);
                    intent.putExtra("payToken",patToken);
                    intent.putExtra("type", Constants.TYPE_BALL_LOTTERY);
                    startActivityForResult(intent,Constants.TYPE_BALL_LOTTERY);
//                    startActivity(intent);
//                    finish();
                }
            });
            presenter.setParams(1,false);
            presenter.getTotalDes();
            presenter.betting();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==Constants.TYPE_BALL_LOTTERY) {
            finish();
        }
    }

    //    @BindView(R.id.lottery_delete_img)
//    ImageView mDeleteImg;
    @OnClick(R.id.lottery_delete_img)
    void deleteBall(){

        if(mEasyBetView.isNoBallCheck()){
            return;
        }

        BaseAlertDialog dialog=new BaseAlertDialog(mContext);
        dialog.setTitle("温馨提示");
        dialog.setMessage("确定清空所选号码吗?");
        dialog.setPositiveButton("确认", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEasyBetView.clearBall();
                mTotalDesTv.setText(Html.fromHtml(mStandardDes));
            }
        });
        dialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        dialog.show();

    }

    @Override
    protected int layout() {
        return R.layout.lottery_trend_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        mTotalDesTv.setText(Html.fromHtml(mStandardDes));
        mEasyBetView.setCallBack(this);
        mTrendSelectView.setCallBack(this);

        manager=getSupportFragmentManager();
        mPresenter=new LotteryTrendPresenter(this);
        mPresenter.loadData();
        setSelection(0);

    }

    public void setSelection(int position) {
        mCurrentPosition=position;
        if(mCurrentPosition==0){
            mBottomTimer.setVisibility(View.VISIBLE);
            mBottomBetting.setVisibility(View.GONE);
        }else{
            mBottomTimer.setVisibility(View.GONE);
            mBottomBetting.setVisibility(View.VISIBLE);
        }
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragments(transaction);
        switch (position){
            case 0:
                if (mFragment0 == null) {
                    mFragment0 = new TrendMainFragment();
                    transaction.add(R.id.fragment, mFragment0);
                    mFragment0.setmType(0);
                }
//                mTrendSelectView.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        mFragment0.initData(mData);
//                    }
//                });
//                mFragment0.initData(mData);
                transaction.show(mFragment0);
                mFragment0.initData(mData);
                break;
            case 1:
                if (mFragment1 == null) {
                    mFragment1 = new TrendChartFragment();
                    mFragment1.setType(0);
                    transaction.add(R.id.fragment, mFragment1);
                }
                mTrendSelectView.post(new Runnable() {
                    @Override
                    public void run() {
                        mFragment1.initData(mData,mPresenter.getBottomVisible());
                    }
                });
//                mFragment1.initData(mData);
                transaction.show(mFragment1);
                break;
            case 2:
                if (mFragment2 == null) {
                    mFragment2 = new TrendChartFragment();
                    mFragment2.setType(1);
                    transaction.add(R.id.fragment, mFragment2);
                }
                mTrendSelectView.post(new Runnable() {
                    @Override
                    public void run() {
                        mFragment2.initData(mData,mPresenter.getBottomVisible());
                    }
                });
//                mFragment2.initData(mData);
                transaction.show(mFragment2);
                break;
            case 3:
                if (mFragment3 == null) {
                    mFragment3 = new TrendMainFragment();
                    transaction.add(R.id.fragment, mFragment3);
                    mFragment3.setmType(3);
                }
//                mTrendSelectView.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        mFragment3.initData(mData);
//                    }
//                });
//                mFragment3.initData(mData);
                transaction.show(mFragment3);
                mFragment3.initData(mData);
                break;
            case 4:
                if (mFragment4 == null) {
                    mFragment4 = new TrendMainFragment();
                    transaction.add(R.id.fragment, mFragment4);
                    mFragment4.setmType(4);
                }
//                mTrendSelectView.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        mFragment4.initData(mData);
//                    }
//                });
//                mFragment4.initData(mData);
                transaction.show(mFragment4);
                mFragment4.initData(mData);
                break;
        }

        transaction.commit();

    }

    private void hideFragments(FragmentTransaction transaction) {

        if (mFragment0 != null) {
            transaction.hide(mFragment0);
        }
        if (mFragment1 != null) {
            transaction.hide(mFragment1);
        }
        if (mFragment2 != null) {
            transaction.hide(mFragment2);
        }
        if (mFragment3 != null) {
            transaction.hide(mFragment3);
        }
        if (mFragment4 != null) {
            transaction.hide(mFragment4);
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void getkTotalDes(String des, boolean correct) {
//        mStandardDes=des;
        mStandardCorrect=correct;
        if(mStandardCorrect){
            mConfirmBtn.setBackgroundResource(R.color.orange_primary);
        }else{
            mConfirmBtn.setBackgroundResource(R.color.divider);
        }
        mTotalDesTv.setText(Html.fromHtml(des));
    }

    @Override
    public void getLottertInfo(LotteryTicketInfo info) {

    }

    @Override
    public void matchingBarSelected(int position) {
        mTrendSelectView.setPageSelected(position);
        setSelection(position);
        smoothScrollTo(position * mTrendSelectView.subTitleWidth);
    }
    /**
     * 流畅滑动
     */
    private void smoothScrollTo(final int scrollTo) {
        final int currentPosition = scroll_view.getScrollX();
        Animation anim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                int scrollOffset = scrollTo - currentPosition;
                scroll_view.scrollTo(currentPosition + (int) (scrollOffset * interpolatedTime), 0);
            }
        };
        anim.setDuration(300);
        scroll_view.startAnimation(anim);
    }

    @Override
    public void setTrendData(TrendChartData data) {
        mData=data;
        String des=mData.getStopTime();
        String[]desDs=des.split("\\|");
        mBottomTimerTv.setText(desDs[0]+"期  截止时间  "+desDs[1]);
        switch (mCurrentPosition){
            case 0:
                mFragment0.initData(mData);
                break;
            case 1:
                mFragment1.initData(mData,mPresenter.getBottomVisible());
                break;
            case 2:
                mFragment2.initData(mData,mPresenter.getBottomVisible());
                break;
            case 3:
                mFragment3.initData(mData);
                break;
            case 4:
                mFragment4.initData(mData);
                break;
        }

    }

    @Override
    public void onPostEvent(CommonEvent event) {
        try {
            switch (event.getWhat()) {
                case EventWhat.EVENT_TREND:
                    String msg=event.getMessage();
                    mPresenter.setParams(msg);
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CommonApp.mLotteryBettingList.clear();
        CommonApp.mLotteryIndex=-1;
    }
}
